package GUILayer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import CtrLayer.SupplierCtr;
import ModelLayer.Supplier;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SupplierUI extends JPanel {
	private JTextField txtSupId;
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtCountry;
	private JTextField txtPhoneNo;
	private JTextField txtEmail;
	private JButton btnCreate;
	private JButton btnUpdate;
	private JButton btnDelete;
	private JButton btnClear;

	private JScrollPane SupplierScrollPane;
	private JTable SupplierTable;
	private SupplierTableModel supTableModel;
	private JTextField txtFilter;

	private SupplierCtr supCtr;
	private JButton btnView;


	public SupplierUI() {
		supCtr = new SupplierCtr();
		setLayout(null);

		JPanel panelEdit = new JPanel();
		panelEdit.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Supplier", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelEdit.setBounds(10, 13, 346, 346);
		add(panelEdit);
		panelEdit.setLayout(null);

		JLabel lblSupId = new JLabel("Id");
		lblSupId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSupId.setBounds(10, 21, 85, 14);
		panelEdit.add(lblSupId);

		txtSupId = new JTextField();
		txtSupId.setBounds(104, 17, 134, 22);
		panelEdit.add(txtSupId);
		txtSupId.setEditable(false);
		txtSupId.setColumns(10);

		JLabel lblName = new JLabel("Name");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setBounds(10, 54, 85, 14);
		panelEdit.add(lblName);


		txtName = new JTextField();
		txtName.setBounds(104, 50, 134, 22);
		panelEdit.add(txtName);
		txtName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableClear();
			}
		});
		txtName.setColumns(10);

		JLabel lblAddress = new JLabel("Address");
		lblAddress.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAddress.setBounds(10, 87, 85, 14);
		panelEdit.add(lblAddress);

		txtAddress = new JTextField();
		txtAddress.setBounds(104, 83, 134, 22);
		panelEdit.add(txtAddress);
		txtAddress.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableClear();
			}
		});
		txtAddress.setColumns(10);

		JLabel lblCountry = new JLabel("Country");
		lblCountry.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCountry.setBounds(10, 120, 85, 14);
		panelEdit.add(lblCountry);

		txtCountry = new JTextField();
		txtCountry.setBounds(104, 116, 134, 22);
		panelEdit.add(txtCountry);
		txtCountry.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableClear();
			}
		});
		txtCountry.setColumns(10);

		JLabel lblPhoneNo = new JLabel("Phone");
		lblPhoneNo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPhoneNo.setBounds(10, 153, 85, 14);
		panelEdit.add(lblPhoneNo);

		txtPhoneNo = new JTextField();
		txtPhoneNo.setBounds(104, 149, 134, 22);
		panelEdit.add(txtPhoneNo);
		txtPhoneNo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableClear();
			}
		});
		txtPhoneNo.setColumns(10);

		JLabel lblEmail = new JLabel("Email");
		lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEmail.setBounds(10, 186, 85, 14);
		panelEdit.add(lblEmail);

		txtEmail = new JTextField();
		txtEmail.setBounds(104, 182, 134, 22);
		panelEdit.add(txtEmail);
		txtEmail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableClear();
			}
		});
		txtEmail.setColumns(10);

		btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createSupplier();
			}
		});
		btnCreate.setBounds(248, 17, 86, 23);
		panelEdit.add(btnCreate);

		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateSupplier();
			}
		});
		btnUpdate.setBounds(248, 50, 86, 23);
		panelEdit.add(btnUpdate);

		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteSupplier();
			}
		});
		btnDelete.setBounds(248, 83, 86, 23);
		panelEdit.add(btnDelete);

		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearInputs();
				enableClear();
			}
		});
		btnClear.setBounds(248, 116, 86, 23);
		btnClear.setEnabled(false);
		panelEdit.add(btnClear);

		JPanel panelTableModel = new JPanel();
		panelTableModel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Suppliers", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelTableModel.setBounds(371, 13, 612, 343);
		add(panelTableModel);
		panelTableModel.setLayout(null);

		SupplierScrollPane = new JScrollPane();
		SupplierScrollPane.setBounds(12, 13, 590, 284);
		SupplierTable = new JTable();
		supTableModel = new SupplierTableModel();
		supTableModel.populateList();
		SupplierTable.setModel(supTableModel);
		SupplierScrollPane.setViewportView(SupplierTable);
		panelTableModel.add(SupplierScrollPane);

		JLabel lblFilter = new JLabel("Filter:");
		lblFilter.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFilter.setBounds(12, 314, 85, 14);
		panelTableModel.add(lblFilter);
		
		txtFilter = new JTextField();
		txtFilter.setBounds(107, 310, 116, 22);
		panelTableModel.add(txtFilter);
		txtFilter.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				filterSuppliers();
			}
		});
		txtFilter.setColumns(10);

		btnView = new JButton("View");
		btnView.setBounds(505, 309, 97, 25);
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewSupplier(SupplierTable.getSelectedRow());
			}
		});
		panelTableModel.add(btnView);
	}

	protected void viewSupplier(int selectedRow) {
		if(selectedRow > -1)
		{
			Supplier sup = supCtr.findSup((int) SupplierTable.getModel().getValueAt(selectedRow, 0));
			txtSupId.setText(String.valueOf(sup.getId()));
			txtName.setText(sup.getName());
			txtAddress.setText(sup.getAddress());
			txtCountry.setText(sup.getCountry());
			txtPhoneNo.setText(sup.getPhoneNo());
			txtEmail.setText(sup.getEmail());
		}
	}

	protected void filterSuppliers() {
		supTableModel.populateList(txtFilter.getText());
	}

	protected void createSupplier() {
		if(isAllInputsFilled(false))
		{
			supCtr.insertSup(txtName.getText(),	txtAddress.getText(),
					txtCountry.getText(), txtPhoneNo.getText(),
					txtEmail.getText());
		}
		supTableModel.populateList();
		clearInputs();
	}

	protected void updateSupplier() {
		if(isAllInputsFilled(true))
		{
			supCtr.updateSup(Integer.parseInt(txtSupId.getText()), txtName.getText(),	txtAddress.getText(),
					txtCountry.getText(), txtPhoneNo.getText(),
					txtEmail.getText());
		}
		supTableModel.populateList();
	}

	protected void deleteSupplier() {
		if(!txtSupId.getText().isEmpty())
		{
			supCtr.removeSup(Integer.parseInt(txtSupId.getText()));
		}
		supTableModel.populateList();
		clearInputs();
	}

	/**
	 * Clear inputs, all are empty.
	 */
	protected void clearInputs() {
		txtSupId.setText("");
		txtName.setText("");
		txtAddress.setText("");
		txtCountry.setText("");
		txtPhoneNo.setText("");
		txtEmail.setText("");
		btnClear.setEnabled(false);
		btnCreate.transferFocus();
	}
	
	/**
	 * Enable of clearing, checks if all inputs are empty.
	 */
	protected void enableClear() {
		btnClear.setEnabled(anyInputFilled());
	}
	/*
	 * Helper function, this is used to avoid code reuse
	 */
	protected boolean anyInputFilled()
	{
		boolean isFilled = false;
		// || is or logic
		if(!txtName.getText().isEmpty() || !txtAddress.getText().isEmpty() ||
				!txtCountry.getText().isEmpty() || !txtPhoneNo.getText().isEmpty() ||
				!txtEmail.getText().isEmpty())
		{
			isFilled = true;
		}
		return isFilled;
	}
	protected boolean isAllInputsFilled(boolean checkId)
	{
		boolean allFilled = false;
		// && is and logic
		if(!txtName.getText().isEmpty() && !txtAddress.getText().isEmpty() &&
				!txtCountry.getText().isEmpty() && !txtPhoneNo.getText().isEmpty() &&
				!txtEmail.getText().isEmpty())
		{
			if(checkId && txtSupId.getText().isEmpty())
			{
				allFilled = false;
			}
			else{
				allFilled = true;
			}
		}
		
		return allFilled;
	}
}